/**
 * 服务
 * */
package org.zmhhxl.user.service.impl;


import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.zmhhxl.api.auth.domain.entity.Role;
import org.zmhhxl.api.auth.domain.entity.dto.RoleDTO;
import org.zmhhxl.common.core.enumate.Enabled;
import org.zmhhxl.user.domain.repository.RoleRepository;
import org.zmhhxl.user.domain.repository.ThirdPartyUserRepository;
import org.zmhhxl.user.domain.repository.UserInfoRepository;
import org.zmhhxl.user.domain.repository.UserRoleRepository;
import org.zmhhxl.user.service.RoleService;

import java.util.List;

//@Transactional(rollbackFor = Exception.class)
@Service
@AllArgsConstructor
public class JpaRoleServiceImpl implements RoleService {

   private final RoleRepository roleRepository;
   private final UserInfoRepository userInfoRepository;
   private final ThirdPartyUserRepository thirdPartyUserRepository;
   //private final RoleMapper roleMapper;
   private final UserRoleRepository userRoleRepository;

   /**
    * Save role.
    *
    * @param role the role
    * @return the role
    */
   @Override
   public Role save(Role role) {
      role.setEnabled(Boolean.parseBoolean(Enabled.ENABLE.val()));
      return roleRepository.save(role);
   }

   /**
    * Update role.
    *
    * @param role the role
    * @return the role
    */
   @Override public Role update(Role role) {
      Role flush = roleRepository.findById(role.getId()).orElseThrow(()->new RuntimeException("角色不存在"));
      //roleMapper.mergeAndIgnorePermissions(role, flush);
      this.roleRepository.flush();
      return flush;
   }

   public Role findById(Long id) {
      return roleRepository.findById(id).orElse(null);
   }

   @Override
   public Role findByRoleName(String roleName) {
      return roleRepository.findByRoleName(roleName);
   }

   /**
    * Delete by id.
    *
    * @param roleId the role id
    */
   @Override
   public void deleteById(Long roleId) {
      roleRepository.deleteById(roleId);
   }

   /**
    * role tree data list.
    *
    * @param userId the user id
    * @return the list
    */
   @Override public List<RoleDTO> roleTreeData(String userId) {
      return null;
   }
}
